//
//  YCSearchViewController.m
//  LOLBox
//
//  Created by mac on 15-3-31.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCSearchViewController.h"
#import "HttpRequestManager.h"
#import "YCNewSModel.h"
#import "YCNewTableViewCell.h"
#import "YCDetailsViewController.h"
#import "MJRefresh.h"
#import "YCAppDelegate.h"
#import "AFNetworking.h"
//w网址
#import "NetInterface.h"

@interface YCSearchViewController ()<MJRefreshBaseViewDelegate,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    NSString *_pathString;
    NSInteger _page;
    
    // 刷新
    MJRefreshFooterView *_footerView;
    MJRefreshHeaderView *_headerView;
    MJRefreshBaseView *_baseView;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation YCSearchViewController

- (void)viewWillAppear:(BOOL)animated
{
    YCAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate hideTabBar];
    animated = YES;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self createBackBarBtnWithTarget:self WithAction:@selector(popTheView)];

    }
    return self;
}

- (void)popTheView
{
    if (self.dataSource.count > 0)
    {
        NSLog(@"清数据");
        [self.dataSource removeAllObjects];
    }
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)senderUrl:(NSString *)path withWhat:(NSString *)what
{
    _page = 1;
    _pathString = path;
    NSLog(@"网址-- %@ --名字: %@",path,what);
    
    [self createNavTitleView:[NSString stringWithFormat:@"搜索 %@",what]];
    
    [self AFN:_pathString page:_page];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    /**
     实例化TableView
     */
    [self initTableView];
    
    [self initFooterView];
    [self initHeaderView];
    
}

//数据下载
- (void)AFN:(NSString *)str page:(NSInteger)page
{
    [[HttpRequestManager shareInstance] getSeaNewsInfo:str andPage:page withSuccess:^(HttpRequestManager *manager, NSMutableArray *arr) {
        //NSLog(@"1");
        self.dataSource = arr;
        [self.tableView reloadData];
        
        [_baseView endRefreshing];
        
    } andFailure:^(HttpRequestManager *manager, NSError *error) {
        NSLog(@"-1");
        
        [_baseView endRefreshing];
    }];
}


- (void)initTableView
{
    //CGRect rect = [UIScreen mainScreen].bounds;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    //self.tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 95;
    //self.tableView.sectionHeaderHeight = 300;
    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    /**
     *  去掉多余的线
     */
    UIView *v = [[UIView alloc] init];
    v.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = v;
    
    // 注册Cell
    [self.tableView registerClass:[YCNewTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.tableView];
    
}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCNewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (self.dataSource.count > 0)
    {
        YCNewSModel *model = self.dataSource[indexPath.row];
        [cell fillCellWithModel:model];
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 95;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YCNewSModel *model = self.dataSource[indexPath.row];
    YCDetailsViewController *details = [[YCDetailsViewController alloc] init];
    //NSLog(@"sss:%@",model.Id);
    self.navigationController.hidesBottomBarWhenPushed = YES;
    [details senderNewsModel:model];
    [self.navigationController pushViewController:details animated:YES];
    //self.tabBarController.tabBar.hidden = YES;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createimg
{
    UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame), 320, 50)];
    v.backgroundColor = [UIColor blackColor];
    [self.view addSubview:v];
}


- (void)dealloc
{
    [_headerView free];
    [_footerView free];
}

- (void)initHeaderView
{
    _headerView = [[MJRefreshHeaderView alloc] initWithScrollView:_tableView];
    _headerView.delegate = self;
}

- (void)initFooterView
{
    _footerView = [[MJRefreshFooterView alloc] initWithScrollView:_tableView];
    _footerView.delegate = self;
}

#pragma mark - MJRefreshBaseViewDelegate

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    _baseView = refreshView;
    
    if (_baseView == _headerView)
    {
        NSLog(@"刷新");
        
        if (_dataSource.count > 0)
        {
            [_dataSource removeAllObjects];
        }
        _page = 1;
    }
    else
    {
        NSLog(@"加载更多");
        _page++;
    }
    
    NSLog(@"see%d",_page);
    [self AFN:_pathString page:_page];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
